//
//  BaseViewController.swift
//  SLQuickReader
//
//  Created by ALin on 2017/8/2.
//  Copyright © 2017年 ALin. All rights reserved.
//

import UIKit
import RxSwift

class BaseViewController: UIViewController {
    
    #if TRACE_RESOURCES
    private let startResourceCount = RxSwift.Resources.total
    private let endResourceCount = RxSwift.Resources.total
    #endif
    
    var disposBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        #if TRACE_RESOURCES
            print("Number of start resources = \(Resources.total)")
        #endif
        
    }
    
    deinit {
        #if TRACE_RESOURCES
            print("\(self) disposed with \(Resources.total) resources")
            
            let numberOfResourcesThatShouldRemain = startResourceCount
            // 设置1秒后, 判断是否还有Observable没有被dispose.
            let when = DispatchTime.now() + DispatchTimeInterval.milliseconds(1000)
            DispatchQueue.main.asyncAfter (deadline: when) {
                assert(Resources.total <= numberOfResourcesThatShouldRemain, "In \(self), Resources weren't cleaned properly, \(Resources.total) remained, \(numberOfResourcesThatShouldRemain) expected")
                
            }
        #endif
    }

}

extension UIViewController{
    // 如果是SB加载的
    static func loadFromSB() -> UIViewController {
       return UIStoryboard(name: self.stringValue(), bundle: nil).instantiateInitialViewController()!
    }
}
